package com.yuexin.project.dailymenu.menumanager.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuexin.common.support.Convert;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.project.dailymenu.menumanager.domain.Manager;
import com.yuexin.project.dailymenu.menumanager.domain.MenuDish;
import com.yuexin.project.dailymenu.menumanager.mapper.ManagerMapper;
import com.yuexin.project.dailymenu.menumanager.mapper.MenuDishMapper;

/**
 * 菜单管理列 服务层实现
 * 
 * @author yuexin
 * @date 2018-10-10
 */
@Service
public class ManagerServiceImpl implements IManagerService 
{
	@Autowired
	private ManagerMapper managerMapper;
	
	@Autowired
	private MenuDishMapper menuDishMapper;

	/**
     * 查询菜单管理列信息
     * 
     * @param menuId 菜单管理列ID
     * @return 菜单管理列信息
     */
    @Override
	public Manager selectManagerById(Integer menuId)
	{
	    return managerMapper.selectManagerById(menuId);
	}
	
	/**
     * 查询菜单管理列列表
     * 
     * @param manager 菜单管理列信息
     * @return 菜单管理列集合
     */
	@Override
	public List<Manager> selectManagerList(Manager manager)
	{
	    return managerMapper.selectManagerList(manager);
	}
	
    /**
     * 新增菜单管理列
     * 
     * @param manager 菜单管理列信息
     * @return 结果
     */
	@Override
	public int insertManager(Manager manager)
	{
		manager.setUpdateBy(ShiroUtils.getLoginName());
	    return managerMapper.insertManager(manager);
	}
	
	/**
     * 修改菜单管理列
     * 
     * @param manager 菜单管理列信息
     * @return 结果
     */
	@Override
	public int updateManager(Manager manager)
	{
		manager.setUpdateBy(ShiroUtils.getLoginName());
	    return managerMapper.updateManager(manager);
	}

	/**
     * 删除菜单管理列对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteManagerByIds(String ids)
	{
		return managerMapper.deleteManagerByIds(Convert.toStrArray(ids));
	}
	
	/**
     * 新增菜单菜品
     *
     * @param 
     */
    public int insertMenuDish(Manager manager)
    {
        int rows = 1;
        List<MenuDish> list = new ArrayList<MenuDish>();
        for (Integer dish : manager.getDishIds())
        {
            MenuDish rd = new MenuDish();
            rd.setMenuId(manager.getMenuId());
            rd.setDishId(dish);
            rd.setDinnerType(manager.getDinnerType());
            rd.setCompanyId(ShiroUtils.getCompanyId());
            list.add(rd);
        }
        if (list.size() > 0)
        {
            rows = menuDishMapper.batchMenuDish(list);
        }
        return rows;
    }

	
	/**
     * 修改菜品信息
     * 
     * @param 
     * @return 结果
     */
    @Override
    public int updateRule(Manager manager)
    {
    	//manager.setUpdateBy(ShiroUtils.getLoginName());
    	
    	String dinnerType = manager.getDinnerType();
    	String isOpen = manager.getIsOpen();
    	if("0".equals(dinnerType)){
    		manager.setIsBreakFast(isOpen);
    	}else if("1".equals(dinnerType)){
    		manager.setIsLunch(isOpen);;
    	}else if("2".equals(dinnerType)){
			manager.setIsDinner(isOpen);
		}else if("3".equals(dinnerType)){
			manager.setIsOther(isOpen);
		}
    	managerMapper.updateManager(manager);
        // 删除菜单与菜品关联
    	menuDishMapper.deleteMenuDishByMenuId(manager);
    	//managerMapper.deleteManagerById(manager.getMenuId());
        // 新增菜单与菜品关联
        return insertMenuDish(manager);
    }

	@Override
	public List<Manager> selectMenuAll(String companyId) {
		
		return managerMapper.selectMenuAll(companyId);
	}

}
